package com.yh.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.pojo.Member;
import com.yh.pojo.Orders;
import com.yh.pojo.Product;
import com.yh.pojo.Traveller;
import com.yh.service.MemberService;
import com.yh.service.OrderService;
import com.yh.service.ProductService;
import com.yh.service.TravellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author by 张晨光
 * @date 2023/11/3 14:32
 */
@Controller
@RequestMapping("order")
public class OrderController {
    //去调用业务层对象; 500
    @Autowired
    OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private TravellerService travellerService;

    /**
     * 从左侧导航栏：order/findAll?page=1&size=5;
     *          传递的参数是：
     *                  page
     *                  size
     * @return
     */
    @GetMapping("findAll")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size",defaultValue = "5") Integer size, Model model){
        PageHelper.startPage(page,size);
        //调用service,获取订单列表;
        List<Orders> ordersList = orderService.findAll();
        //经过分页插件过滤;
        PageInfo<Orders>pageInfo=new PageInfo<>(ordersList);
        System.out.println(pageInfo);
        //还是设置pageInfo
        model.addAttribute("pageInfo",pageInfo);
//        //去页面了;
        return "order-list";
    }
    @GetMapping("findById")
    public String findById(@RequestParam("id") Integer id,Model model){
        System.out.println(id);
        Orders order = orderService.findById(id);
        //刚才页面渲染，没有数据，则来控制器，来查看。
        model.addAttribute("order",order);  //?刚才暂停了一下;
        return "order-show";    // 详情页面;
    }

    /**
     * 这里最好的命名是toAdd,表示去某个页面;
     * @return
     */
    @GetMapping("add")
    public String toAdd(Model model){
        //在去添加页面之前，调用service,获取内容显示;
        List<Product> productList = productService.findAll();
        List<Member>  memberList = memberService.findAll();
        List<Traveller> travellerList = travellerService.findAll();
        //将上面的数据添加到model里面;
        model.addAttribute("productList",productList);
        model.addAttribute("memberList",memberList);
        model.addAttribute("travellerList",travellerList);
        //下面页面转向;
        return "order-add";
    }
    @PostMapping("save")
    public String save(Orders orders){
        //这里Orders,需要修改一下;
        orderService.save(orders,orders.getTravellerId());
        return "redirect:findAll";
    }
}
